﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ServiceStack.OrmLite;
using ServiceStack.Common.Utils;

namespace Intex.Db.Models
{
    public class DbContext : IDisposable
    {

        public OrmLiteConnectionFactory factory;

        private System.Data.IDbConnection db;

        public System.Data.IDbConnection Items
        {
            get
            {
                return db;
            }
        }

        public DbContext()
        {
            if(factory==null) 
            {
              
                factory=new OrmLiteConnectionFactory(
                Config.DbFile, false, SqliteDialect.Provider);
                factory.AutoDisposeConnection = true;
            }
            db = factory.Open();            
            db.CreateTableIfNotExists<ItemType>();
            db.CreateTableIfNotExists<ItemTemplate>();
            db.CreateTableIfNotExists<ItemTemplateValue>();
            db.CreateTableIfNotExists<Item>();
            db.CreateTableIfNotExists<ItemValue>();

        }


        public void Dispose()
        {
            db.Close();
        }
    }

    public class Config
    {
        private static string _dbFile;
        private static string _imageTempPath;
        private static Dictionary<string, string> _templatefiles;
        private static string _baseId;
        private static int _indexLength;

        public static int IndexLength { get { return _indexLength; } }
        public static string DbFile
        {
            get
            {
                return _dbFile;
            }
        }

        public static string ImageTempPath
        {
            get
            {
                return _imageTempPath;
            }
        }

        public static string BaseId
        {
            get
            {
                return _baseId;
            }
        }

        public static bool IsDebug
        {
            get
            {
                return _isDebug;
            }
        }

        private static string _nameAlias;
        private static string _createdAlias;
        private static string _fundamentalDataColumnPrefix;
        private static string _displayedColumn;
        private static string _indexAlias;
        private static string _fontName;
        private static int _fontSize;
        private static bool _fontBold;
        private static int _codeHeigth;
        private static int _codeWidth;
        private static int _codeMarginHeight;
        private static string _localColumn;
        private static int _codeMarginWidth;
        private static int _maxDay;
        private static bool _isCheckedWhenSimplePrinter;
        private static bool _isDebug = false;



        public static string LocalColumn { get { return _localColumn; } }
        public static string FontName { get { return _fontName; } }
        public static bool FontBold { get { return _fontBold; } }
        public static int FontSize { get { return _fontSize; } }
        public static int CodeHeight { get { return _codeHeigth; } }
        public static int CodeWidth { get { return _codeWidth; } }
        public static int CodeMarginWidth { get { return _codeMarginWidth; } }
        public static int CodeMarginHeight { get { return _codeMarginHeight; } } 

        public static string IndexAlias { get { return _indexAlias; } }
        public static string NameAlias { get { return _nameAlias; } }

        public static string DisplayedColumn { get { return _displayedColumn; } }

        public static string ConfigFilePath { get { return "~/Config/".MapAbsolutePath(); } }//= @".\Config\";
        public static string TemplateFilePath { get { return "~/Template/".MapAbsolutePath(); } } // = @".\Template\";
        public static string TempFilePath { get { return "~/TempFile/".MapAbsolutePath(); } }  //= @".\TempFile\";

        public static int MaxDay { get { return _maxDay; } }
        public static string CreatedAlias { get { return _createdAlias; } }
        public static string FundamentalDataColumnPrefix { get { return _fundamentalDataColumnPrefix; } }

        public static string DBBackupPath
        {
            get
            {
                string backpath = "~/Backup/".MapAbsolutePath();
                if (!System.IO.Directory.Exists(backpath))
                {
                    System.IO.Directory.CreateDirectory(backpath);
                }
                return backpath;
            }
        }

        public static List<string> GetShowConlum(string typeName)
        {
            if (!_templatefiles.ContainsKey(typeName)) return new List<string>();
            List<string> list = new List<string>();
            list.AddRange(_templatefiles[typeName].Split(new char[]{','}, StringSplitOptions.RemoveEmptyEntries));
            list = list.Distinct().ToList();
            list.Remove(_fundamentalDataColumnPrefix + "5");
            return list;
        }

        public static bool IsCheckedWhenSimplePrinter
        {
            get
            {
                return _isCheckedWhenSimplePrinter;
            }
        }

        private static int _codeFontPosY;

        public static int CodeFontPosY
        {
            get
            {
                return _codeFontPosY;
            }
        }

        public static void Create()
        {
            _dbFile = System.Configuration.ConfigurationManager.AppSettings["DbfileName"];
            _imageTempPath = System.Configuration.ConfigurationManager.AppSettings["ImageTempPath"];
            _imageTempPath = ("~/" + _imageTempPath).MapAbsolutePath();
            string showConlumn = System.Configuration.ConfigurationManager.AppSettings["ShowColumn"];
            _nameAlias = System.Configuration.ConfigurationManager.AppSettings["NameAlias"];
            _createdAlias = System.Configuration.ConfigurationManager.AppSettings["CreatedDateAlias"];
            _templatefiles = new Dictionary<string, string>();
            _baseId = System.Configuration.ConfigurationManager.AppSettings["BaseId"];
            _fundamentalDataColumnPrefix = System.Configuration.ConfigurationManager.AppSettings["FundamentalDataColumnPrefix"];
            _indexAlias = System.Configuration.ConfigurationManager.AppSettings["IndexAlias"];
            _indexLength = int.Parse(System.Configuration.ConfigurationManager.AppSettings["IndexLength"]);
            _fontName = System.Configuration.ConfigurationManager.AppSettings["FontName"];
            _fontBold = Convert.ToBoolean(System.Configuration.ConfigurationManager.AppSettings["CodeFontBold"]);
            _fontSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CodeFontSize"]);
            _codeHeigth = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CodeHeight"]);
            _codeWidth = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CodeWidth"]);
            _codeMarginHeight = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CodeMarginHeight"]);
            _codeMarginWidth = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CodeMarginWidth"]);
            _codeFontPosY = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CodeFontPosY"]);
            _localColumn = System.Configuration.ConfigurationManager.AppSettings["LocalColumn"];
            _maxDay = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["MaxDay"]);
            if (!Boolean.TryParse(System.Configuration.ConfigurationManager.AppSettings["IsDebug"], out _isDebug))
            {
                _isDebug = false;
            }
            if (!System.IO.Directory.Exists(_imageTempPath))
            {
                System.IO.Directory.CreateDirectory(_imageTempPath);
            }
            var conlumns = showConlumn.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in conlumns)
            {
                var keyvalue = item.Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                if (keyvalue.Length < 1) continue;
                _templatefiles.Add(keyvalue[0], keyvalue[1]);
            }
            _displayedColumn = System.Configuration.ConfigurationManager.AppSettings["DisplayColumn"];
            _dbFile = ("~/Data/" + _dbFile).MapAbsolutePath();
            if (!Boolean.TryParse(System.Configuration.ConfigurationManager.AppSettings["SimpleCheck"], out _isCheckedWhenSimplePrinter))
            {
                _isCheckedWhenSimplePrinter = false;
            }
        }
    }
}
